//
//  CZStatusToolBar.swift
//  WeiBo
//
//  Created by Apple on 16/9/6.
//  Copyright © 2016年 KY. All rights reserved.
//

import UIKit

//  首页toolbar视图
class CZStatusToolBar: UIView {

    // 绑定数据模型
    var statusViewModel: CZStatusViewModel? {
        didSet {
            
//            //  转发
//            let retweetCount = statusViewModel?.status?.reposts_count ?? 0
//            //  评论
//            let commentCount = statusViewModel?.status?.comments_count ?? 0
//            //  赞
//            let unlikeCount = statusViewModel?.status?.attitudes_count ?? 0
            
            retweetButton.setTitle(statusViewModel?.retweetCountContent, for: .normal)
            commentButton.setTitle(statusViewModel?.commentCountContent, for: .normal)
            unlikeButton.setTitle(statusViewModel?.unlikeCountContent, for: .normal)
        }
    }
    
    lazy var retweetButton: UIButton = self.addChildButton(title: "转发", imageName: "timeline_icon_retweet")
    lazy var commentButton: UIButton = self.addChildButton(title: "评论",imageName: "timeline_icon_comment")
    lazy var unlikeButton: UIButton = self.addChildButton(title: "赞",imageName: "timeline_icon_unlike")
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI() {
    
        
        
        //  设置约束
        retweetButton.snp_makeConstraints { (make) in
            make.leading.equalTo(self)
            make.top.equalTo(self)
            make.bottom.equalTo(self)
            make.width.equalTo(commentButton)
        }
        commentButton.snp_makeConstraints { (make) in
            make.leading.equalTo(retweetButton.snp_trailing)
            make.top.equalTo(self)
            make.bottom.equalTo(self)
            make.width.equalTo(unlikeButton)
        }
        unlikeButton.snp_makeConstraints { (make) in
            make.leading.equalTo(commentButton.snp_trailing)
            make.top.equalTo(self)
            make.bottom.equalTo(self)
            make.trailing.equalTo(self)
        }
        
        //  添加线视图
        let firstLineView = addLineView()
        let secondLineView = addLineView()
        
        firstLineView.snp_makeConstraints { (make) in
            make.centerX.equalTo(retweetButton.snp_trailing)
            make.centerY.equalTo(self)
        }
        secondLineView.snp_makeConstraints { (make) in
            make.centerX.equalTo(commentButton.snp_trailing)
            make.centerY.equalTo(self)
        }
        
        
    }
    
    //  添加子控件
    private func addChildButton(title:String, imageName: String) -> UIButton {
    
        let button = UIButton()
        button.setTitle(title, for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setImage(UIImage(named: imageName), for: .normal)
        //  去掉高亮颜色
        button.adjustsImageWhenHighlighted = false
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), for: .normal)
        
        addSubview(button)
        
        return button
    }
    
    //  添加子线
    private func addLineView() -> UIImageView {
        let imageView = UIImageView(image: UIImage(named: "timeline_card_bottom_line"))
        addSubview(imageView)
        return imageView
    }
    
    
    

}
